package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.*;
import com.sg.entity.CheckItem;
import com.sg.service.base.app.MCheckItemService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.CheckItemMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MCheckItemServiceImpl
        extends ServiceImpl<CheckItemMapper, CheckItem>
        implements MCheckItemService {

    @Autowired
    CheckItemMapper checkItemMapper;

    /**
     * 3-11-03查询检查项列表[883]
     * gen by moon at 12/31/2022, 7:12:29 PM
     */
    @Trace(operationName = "3-11-03查询检查项列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<CheckItem> queryCheckItemList(QueryCheckItemListReq reqDto) {

        try {
            QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(CheckItem.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(CheckItem.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(CheckItem.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<CheckItem>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-11-03查检查项详情[882]
     * gen by moon at 3/30/2023, 12:38:36 PM
     */
    @Trace(operationName = "3-11-03查检查项详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CheckItem queryCheckItemDetail(QueryCheckItemDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<CheckItem> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                CheckItem retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-11-03新增检查项[881]
     * gen by moon at 12/31/2022, 7:12:27 PM
     */
    @Trace(operationName = "3-11-03新增检查项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addCheckItem(CheckItem checkItem) {
        checkItem.setCheckItemId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        checkItem.setIsArchive("FALSE");
        checkItem.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        checkItem.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (checkItem != null && checkItem.getSubjectLifeCycle() != null && checkItem.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            checkItem.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        checkItem.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        checkItem.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (checkItem != null && checkItem.getSubjectLifeCycle() != null && checkItem.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            checkItem.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        checkItem.setIsValid("FALSE");
        checkItem.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        checkItem.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(checkItem)) {
            return checkItem.getCheckItemId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-11-03修改检查项[880]
     * gen by moon at 12/31/2022, 7:12:27 PM
     */
    @Trace(operationName = "3-11-03修改检查项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCheckItem(CheckItem checkItem) {

        return super.updateById(checkItem);
    }

    /**
     * 3-11-03删除检查项[879]
     * gen by moon at 12/31/2022, 7:12:26 PM
     */
    @Trace(operationName = "3-11-03删除检查项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCheckItem(String checkItemId) {

        return super.removeById(checkItemId);
    }

    /**
     * 3-11-03批量查检查项[2718]
     * gen by moon at 12/31/2022, 7:12:29 PM
     */
    @Trace(operationName = "3-11-03批量查检查项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<CheckItem> batchQueryCheckItem(BatchQueryCheckItemReq reqDto) {

        try {
            QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getCheckItemList() == null || reqDto.getCheckItemList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("check_item_code", reqDto.getCheckItemList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(CheckItem.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(CheckItem.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(CheckItem.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<CheckItem>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-11-03批量查询检查项列表ByIds[4971]
     * gen by moon at 12/31/2022, 7:12:30 PM
     */
    @Trace(operationName = "3-11-03批量查询检查项列表ByIds")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<CheckItem> batchQueryCheckItemListByIds(BatchQueryCheckItemListByIdsReq reqDto) {

        try {
            QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getCheckItemList() == null || reqDto.getCheckItemList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("check_item_id", reqDto.getCheckItemList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(CheckItem.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(CheckItem.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(CheckItem.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<CheckItem>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M3-查询检查项单条测试[5536]
     * gen by moon at 3/30/2023, 12:38:36 PM
     */
    @Trace(operationName = "M3-查询检查项单条测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CheckItem queryCheckItemDataDetail(QueryCheckItemDataDetailReq reqDto) {

        try {

            QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<CheckItem> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                CheckItem retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
